﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tools;
using Ext.Net;
using Itop.GpsCar.Web;
using Itop.GpsCar.Model;
namespace Web.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        MsgBox mb = new MsgBox();

        protected void ImgBtnLogin_Click(object sender, ImageClickEventArgs e)
        {
            if (TxtUserName.Text.Trim().Length <= 0 || TxtPwd.Text.Trim().Length <= 0)
                MessageBoxHelper.ResponseScript(this, "ZENG.msgbox.show(GPS.lang.s_Title_LoginNameNotNull, 1, 1500);");
            else
            {
                var userinfoMgr = Global.GetService<Itop.GpsCar.BLL.userBLL>();
                user user = userinfoMgr.GetByUsername(Server.HtmlEncode(TxtUserName.Text.Trim().Replace("'", "")));
                
                if (user != null && user.password.Trim() == TxtPwd.Text.Trim())
                {
                    CookieHelper.SaveCookie("username", user.username, 0);
                    CookieHelper.SaveCookie("cn_name", user.firstname, 0);
                    CookieHelper.SaveCookie("role", user.roleid, 0);
                    
                    //记录用户当前登录时间
                    userinfoMgr.UpdateLoginTime(user);
                    log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType).Info(string.Format("来自IP" + Request.UserHostAddress + "的用户【{0}】登录系统", user.username));
                    Response.Redirect("/Admin");
                }
                else
                    MessageBoxHelper.ResponseScript(this, "ZENG.msgbox.show(GPS.lang.s_Title_LoginPwdError, 5, 1500);");
            }
        }

        
    }
}